package com.ciwong.epaper.modules.homework.ui;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.android.volley.TimeoutError;
import com.ciwong.epaper.R;
import com.ciwong.epaper.application.EApplication;
import com.ciwong.epaper.modules.epaper.dao.EpaperDao;
import com.ciwong.epaper.modules.homework.adapter.DkWorkAdapter;
import com.ciwong.epaper.modules.homework.bean.PublishDKWorkRecords;
import com.ciwong.epaper.modules.me.bean.PublishDKWorkRecordsPageBean;
import com.ciwong.epaper.ui.BaseFragment;
import com.ciwong.epaper.util.BaseExtCallBack;
import com.ciwong.epaper.widget.PullToRefreshView;
import com.ciwong.libs.utils.NetworkUtils;
import com.ciwong.libs.widget.CWToast;
import com.ciwong.mobilelib.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * @description: 打卡作业列表
 * @author: wyk
 * @time: 2021/10/8 9:17
 */
public class DkWorkFragment extends BaseFragment implements PullToRefreshView.OnHeaderRefreshListener, PullToRefreshView.OnFooterRefreshListener,View.OnClickListener{

    private final int defult_page = 1;

    /**
     * 当前页码
     */
    private int curPage = defult_page;

    /**
     * 默认加载数据大小
     */
    private static final int LOAD_PAGE_SIZE = 15;

    private RelativeLayout no_dk_work;

    private PullToRefreshView my_work_refresh_view;

    private ListView my_dk_work_exlistv;

    private LinearLayout publish_work_lay;
    
    private List<PublishDKWorkRecords> publishDKWorkRecords = new ArrayList<>();
    
    private DkWorkAdapter dkWorkAdapter;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_dk_work, container, false);
        initView(view);
        init();
        return view;
    }

    protected void initView(View view){
        no_dk_work = view.findViewById(R.id.no_dk_work);
        my_work_refresh_view = view.findViewById(R.id.my_work_refresh_view);
        my_dk_work_exlistv = view.findViewById(R.id.my_dk_work_exlistv);
        publish_work_lay = view.findViewById(R.id.publish_work_lay);

        showCricleProgress();
        getDKWorkRecords(curPage);
    }

    protected void init(){
        dkWorkAdapter = new DkWorkAdapter(mActivity,publishDKWorkRecords);
        my_dk_work_exlistv.setAdapter(dkWorkAdapter);
        my_work_refresh_view.setOnHeaderRefreshListener(this);
        my_work_refresh_view.setOnFooterRefreshListener(this);

    }

    /**
     * 获取作业记录
     * @param curPage
     */
    private void getDKWorkRecords(int curPage){
        EpaperDao.getInstance().getDkWorkRecords(EApplication.BRAND_ID,curPage,LOAD_PAGE_SIZE,3,new BaseExtCallBack(mActivity,EApplication.getInstance().getUserInfoBase().getUserId() + ""){
            @Override
            public void failed(Object data) {
                hideCricleProgress();
                my_work_refresh_view.onFooterRefreshComplete();
                my_work_refresh_view.onHeaderRefreshComplete();
                if (data instanceof TimeoutError) {
                    hideCricleProgress();
                    ToastUtil.INSTANCE.toastCenterError(getString(R.string.load_faild_check_net_work));
                }
            }

            @Override
            public void failed(int errorCode, Object data) {
                hideCricleProgress();
                my_work_refresh_view.onFooterRefreshComplete();
                my_work_refresh_view.onHeaderRefreshComplete();
                super.failed(errorCode, data);
                failed(data);
            }

            @Override
            public void success(Object data) {
                super.success(data);
                hideCricleProgress();
                my_work_refresh_view.onFooterRefreshComplete();
                my_work_refresh_view.onHeaderRefreshComplete();
                if (null != data) {
                    PublishDKWorkRecordsPageBean publishDKWorkRecordsPageBean = (PublishDKWorkRecordsPageBean) data;
                    setWorkList(publishDKWorkRecordsPageBean);
                }
            }
        });
    }
    
    private void setWorkList(PublishDKWorkRecordsPageBean publishDKWorkRecordsPageBean){
        try {
            List<PublishDKWorkRecords> pageList = publishDKWorkRecordsPageBean.getPageList();
            int totalCount = publishDKWorkRecordsPageBean.getTotalCount();
            if (0 == totalCount) {
                no_dk_work.setVisibility(View.VISIBLE);
                publish_work_lay.setVisibility(View.GONE);
            } else {
                publishDKWorkRecords = pageList;
                no_dk_work.setVisibility(View.GONE);
                publish_work_lay.setVisibility(View.VISIBLE);
                if (curPage == 1) {
                    dkWorkAdapter.clear();
                    init();
                } else {
                    dkWorkAdapter.addAll(pageList);
                    //my_work_exlistv.setSelectionFromTop(firstPostion,top);
                }

                if (dkWorkAdapter.getCount() == publishDKWorkRecordsPageBean.getPageCount()) {
                    my_work_refresh_view.setFooterState(PullToRefreshView.PULL_TO_NO_MORE);
                } else {
                    my_work_refresh_view.setFooterState(PullToRefreshView.PULL_TO_REFRESH);
                }
            }


        }catch (Exception e){

        }
    }

    @Override
    public void onClick(View view) {

    }

    @Override
    public void onFooterRefresh(PullToRefreshView view) {
        if (!NetworkUtils.isOnline()) {
            CWToast.makeText(mActivity, getString(R.string.connect_disable), Toast.LENGTH_LONG).setToastType(CWToast.TOAST_TYPE_ERROR).show();
            return;
        }
        curPage++;
        getDKWorkRecords(curPage);
    }

    @Override
    public void onHeaderRefresh(PullToRefreshView view) {
        curPage = 1;
        getDKWorkRecords(curPage);
    }
}
